//Copyright 2011 New York City 3D Community

//This file is part of New York City 3D.

//New York City 3D is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//New York City 3D is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with New York City 3D.  If not, see <http://www.gnu.org/licenses/>.

package nyc3d.gui;

import de.lessvoid.nifty.EndNotify;
import nyc3d.updates.UpdateManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.button.builder.CreateButtonControl;
import de.lessvoid.nifty.controls.checkbox.CheckboxControl;

import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.layout.align.HorizontalAlign;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import nyc3d.BareBonesBrowserLaunch;
import nyc3d.updates.UpdateManager.Status;

public class UpdatesScreen implements ScreenController {
  private Nifty nifty;
  private Screen screen;
  private Status updateStatus = Status.CURRENT;

  public void bind(final Nifty newNifty, final Screen newScreen) {
    screen = newScreen;
    nifty = newNifty;

    updateStatus = UpdateManager.checkUpdate();

    screen.findElementByName("currentVersion").getRenderer(TextRenderer.class).setText(UpdateManager.gameVersion + " " + UpdateManager.modifier);
    screen.findElementByName("currentVersion").getRenderer(TextRenderer.class).setTextHAlign(HorizontalAlign.left);

    screen.findElementByName("latestVersion").getRenderer(TextRenderer.class).setText(UpdateManager.latestGameVersion + " " + UpdateManager.latestModifier);
    screen.findElementByName("latestVersion").getRenderer(TextRenderer.class).setTextHAlign(HorizontalAlign.left);

    if (updateStatus.equals(Status.NOT_CURRENT)){
        CreateButtonControl createBackButton = new CreateButtonControl("backButton2");
        createBackButton.set("label", "Download Latest Version");
        createBackButton.setWidth("200px");
        createBackButton.setAlign("right");
        createBackButton.setInteractOnClick("downloadLatestVersion()");
        createBackButton.create(newNifty, screen, screen.findElementByName("buttonPanel"));
    }

    CreateButtonControl createBackButtonSave = new CreateButtonControl("backButton");
    createBackButtonSave.set("label", "Return");
    createBackButtonSave.setAlign("right");
    createBackButtonSave.setInteractOnClick("returnToAboutMenu()");
    createBackButtonSave.create(newNifty, screen, screen.findElementByName("buttonPanel"));
  }

  public void onStartScreen() {
  }

  public void onEndScreen() {
  }

  public void downloadLatestVersion(){
      nifty.createPopup("updatePrompt");
      nifty.showPopup(screen, "updatePrompt", null);
  }

 public void update(final String update) {
      nifty.closePopup("updatePrompt", new EndNotify() {
      public void perform() {
        if ("yes".equals(update)) {
          nifty.exit();
          BareBonesBrowserLaunch.openURL(UpdateManager.downloadURL);
          System.exit(0);
        }
      }
      }
    );

  }

  public void returnToAboutMenu(){
      nifty.fromXml("about.xml", "about");
  }

  private CheckboxControl findCheckboxControl(final String id) {
    CheckboxControl checkBox = screen.findControl(id, CheckboxControl.class);
    return checkBox;
  }
}
